//获取当前的年月日
function getYmd() {
    const date = new Date(); //实例化一个日期对象
    let year = date.getFullYear(); //获取年
    let month = date.getMonth() + 1; //获取月
    let day = date.getDate(); //获取几号
    if (month <= 9) {
        month = "0" + month;
    }
    if (day <= 9) {
        day = "0" + day;
    }
    return year + "-" + month + "-" + day;
}

//页面正则表达式
let validation = {
    regEn: /[`~!@#$%^&*()_+<>?:"{},.\/;'[\]]/im, //英文非法字符
    regCn: /[·！#￥（——）：；“”‘、，|《。》？、【】[\]]/im, //中文非法字符
    phone: /^[1][3,4,5,6,7,8,9][0-9]{9}$/, //手机正则验证
    notEmpty: /\s+/g, //空
    pwd: /^(?![\d]+$)(?![a-zA-Z]+$)(?![^\da-zA-Z]+$).{6,20}$/ //6-20位数字/字母/字符组合
}

//防止重复点击
function throttle(fn, gapTime) {
    if (gapTime == null || gapTime == undefined) {
        gapTime = 3000
    }

    let _lastTime = null

    // 返回新的函数
    return function() {
        let _nowTime = +new Date()
        if (_nowTime - _lastTime > gapTime || !_lastTime) {
            fn.apply(this, arguments) //将this和参数传给原函数
            _lastTime = _nowTime
        }
    }
}

//数组去重复
function uniqArray(array) {
    var temp = []; //一个新的临时数组
    for (var i = 0; i < array.length; i++) {
        if (temp.indexOf(array[i]) == -1) {
            temp.push(array[i]);
        }
    }
    return temp;
}

function getImage(url) {
    // 把现在的图片链接传进来，返回一个不受限制的路径
    if (url !== undefined) {
        return url.replace(/^(http)[s]*(\:\/\/)/, 'https://images.weserv.nl/?url=');
    }
}

//导出模块
module.exports.getYmd = getYmd;
module.exports.validation = validation;
module.exports.throttle = throttle;
module.exports.uniqArray = uniqArray;
module.exports.getImage = getImage;